package com.shujia.mr.scoreSort;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.file.Watchable;

public class StudentSort implements WritableComparable<StudentSort> {
    String id;
    String name;
    int age;
    String gender;
    String clazz;
    int score;

    public StudentSort() {
    }

    public StudentSort(String id, String name, int age, String gender, String clazz, int score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", clazz='" + clazz + '\'' +
                ", score=" + score +
                '}';
    }


    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.id); // HADOOP中UTF 表示的就是一个字符串
        out.writeUTF(this.name);
        out.writeInt(this.age);
        out.writeUTF(this.gender);
        out.writeUTF(this.clazz);
        out.writeInt(this.score);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readUTF();
        this.name = in.readUTF();
        this.age = in.readInt();
        this.gender = in.readUTF();
        this.clazz = in.readUTF();
        this.score = in.readInt();
    }



    @Override
    public int compareTo(StudentSort that) {
//        int res = o1.score - o2.score;
//        return -(res == 0 ? o1.id.compareTo(o2.id) : res);
        // 作业：分数是倒序排序，ID是正序排序
        int res = this.score - that.score;
        return -(res == 0 ? this.id.compareTo(that.id) : res);
    }
}
